package top.aicats.blog.BlogManage.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.aicats.blog.BlogManage.model.BlogContent;
import top.aicats.blog.BlogManage.model.ErrorCode;
import top.aicats.blog.BlogManage.model.ResultObj;
import top.aicats.blog.BlogManage.service.IBlogContentService;

import java.util.List;

/**
 * @author sjc
 * @version 1.0
 * @date 2020/1/11 15:43
 * @email 952491025@qq.com
 */
@RestController
@RequestMapping("/blogContent")
public class BlogContentManageControl {

    @Autowired
    IBlogContentService blogContentService;

    @PostMapping("/addOneBlog")
    @ResponseBody
    public ResultObj addOneBlog(BlogContent content) {
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            Integer resNum = blogContentService.insertOneBlog(content);
            if (resNum > 0) {
                resultObj.setErrorCode(ErrorCode.OK);
                resultObj.setErrorContent("成功添加博客");
            }

        } catch (Exception ex) {
            resultObj.setErrorContent("添加博客失败");
        }
        return resultObj;
    }

    @ResponseBody
    @RequestMapping("/getAllSubBlogTitleByRefid")
    public ResultObj getAllSubBlogTitleByRefid(String blogContentForeignKey) {
        BlogContent content = new BlogContent();
        content.setBlogContentForeignKey(blogContentForeignKey);
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            List<BlogContent> blogContentList = blogContentService.getAllBlogTitleBySubRefid(content);
            resultObj.setErrorCode(ErrorCode.OK);
            resultObj.setData(blogContentList);
        } catch (Exception ex) {
            resultObj.setErrorContent(ex.toString());
        }
        return resultObj;
    }

    @ResponseBody
    @RequestMapping("/getBlogContentByRefid")
    public ResultObj getBlogContentByRefid(String blogRefid) {
        BlogContent content = new BlogContent();
        content.setBlogContentRefid(blogRefid);
        ResultObj resultObj = new ResultObj();
        resultObj.setErrorCode(ErrorCode.ERROR);
        try {
            List<BlogContent> blogContentList = blogContentService.getBlogContentByRefid(content);
            resultObj.setErrorCode(ErrorCode.OK);
            resultObj.setData(blogContentList);
        } catch (Exception ex) {
            resultObj.setErrorContent(ex.toString());
        }
        return resultObj;
    }

    @RequestMapping("/getBlogTitleInfoByPaging")
    @ResponseBody
    public ResultObj getBlogTitleInfoByPaging(@RequestParam("pageSize") Integer pageSize, @RequestParam("page") Integer page, @RequestParam("sortField") String sortField, @RequestParam("desc") Boolean desc) {
        if (pageSize == null) pageSize = 10;
        if (page == null) page = 1;
        if (desc == null) desc = false;
        ResultObj resultObj = new ResultObj();
        try {
            List<BlogContent> blogContentList = blogContentService.getBlogTitleInfoByPaging(pageSize, page, sortField, desc);
            resultObj.setErrorCode(ErrorCode.OK);
            resultObj.setData(blogContentList);
        } catch (Exception ex) {
            resultObj.setErrorContent(ex.toString());
        }
        return resultObj;
    }
}
